package com.app.OOS.mapper;


import java.util.List;

import javax.validation.metadata.ValidateUnwrappedValue;

import org.apache.ibatis.annotations.Insert;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Select;
import org.apache.ibatis.annotations.Update;

import com.app.OOS.domain.Batch_Coursewares;
import com.app.OOS.domain.Score;
import com.app.OOS.domain.User;
import com.app.OOS.domain.User_courseware_batch;



@Mapper
public interface UserMapper {
	//用户注册
	@Insert("insert into user(userId,password,userName,type,setting)values(#{userId},#{password},#{userName},0,0)")
	public void insertUser(@Param("userId")int userId,@Param("password")String password,@Param("userName")String userName);
	//用户登陆
	@Select("select * from user where userId=#{userId} and password=#{password} and type=#{type}")
	public User selectUser(User user);
	//修改密码
	@Update("update user set password=#{password} where userId=#{userId}")
	public void updatePassword(User user);
	//查找学员-批次-课件
	@Select("select * from batch b,batch_courseware bc,user u,user_batch ub,courseware c where u.userId=ub.userId and ub.batchId=b.batchId and ub.batchId=bc.batchId and bc.coursewareId=c.coursewareId and u.userId=#{userId} limit #{tmp},#{limit}")
	public List<Batch_Coursewares> selectBacthNameCoursewareName(@Param("userId")Object object,@Param("tmp")int tmp,@Param("limit")int limit);
	@Select("select count(*) from batch b,batch_courseware bc,user u,user_batch ub,courseware c where u.userId=ub.userId and ub.batchId=b.batchId and ub.batchId=bc.batchId and bc.coursewareId=c.coursewareId and u.userId=#{userId}")
	public int tol(@Param("userId")Object object);
	//查找视频src
	@Select("select coursewareSrc from courseware where coursewareId=#{coursewareId}")
	public String selectSrc(@Param("coursewareId")int coursewareId);
	//查成绩
	@Select("select * from score where userId=#{userId}")
	public List<Score> selectScore1(@Param("userId")Object object);
	//查询学习记录
	@Select("select * from record r,batch b,courseware c where r.batchId=b.batchId and r.coursewareId=c.coursewareId and  r.userId=#{userId}")
	public List<User_courseware_batch> selectRecod(@Param("userId")Object object);
	//插入记录
	@Insert("insert into record(userId,coursewareId,batchId) values(#{userId},#{coursewareId},#{batchId})")
	public void insertStudy(@Param("userId")Object object,@Param("coursewareId")int coursewareId,@Param("batchId")int batchId);
	//查询
	@Select("select count(*) from record where userId=#{userId} and coursewareId=#{coursewareId} and batchId=#{batchId}")
	public int tol8(@Param("userId")Object object,@Param("coursewareId")int coursewareId,@Param("batchId")int batchId);
	
	//查询时间
	@Select("select time from record where userId=#{userId} and coursewareId=#{coursewareId} and batchId=#{batchId}")
	public int time(@Param("userId")Object object,@Param("coursewareId")int coursewareId,@Param("batchId")int batchId);


	//更新时间
	@Update("update record set time=#{time} where userId=#{userId} and coursewareId=#{coursewareId} and batchId=#{batchId}")
	public void updatetime(@Param("userId")Object object,@Param("coursewareId")int coursewareId,@Param("batchId")int batchId,@Param("time")int time);

	//更新状态
	@Update("update record set state='已完成' where time>=0.6*(select Ctime from courseware where coursewareId=#{coursewareId}) and userId=#{userId} and batchId=#{batchId}")
	public void updateState(@Param("userId")Object object,@Param("coursewareId")int coursewareId,@Param("batchId")int batchId);


	//参加考试
	
	@Select("select count(*) from batch b,batch_courseware bc,user u,user_batch ub,courseware c where u.userId=ub.userId and ub.batchId=b.batchId and ub.batchId=bc.batchId and bc.coursewareId=c.coursewareId and u.userId=#{userId}")
	public int tol9(@Param("userId")Object object);
	@Select("select count(*) from record where userId=#{userId} and stste='已完成'")
	public int tol10(@Param("userId")Object object);

}
